package com.ajk.shop.dao.provider;

import com.ajk.shop.model.Category;
import com.ajk.shop.model.Goods;
import com.ajk.shop.model.GoodsCategory;
import com.ajk.shop.model.request.GoodsReq;


/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2016年1月8日 上午11:10:36 
 * 类说明 
 */
public class GoodsMapperProvider {
	
	public String goodsHomeList(final GoodsReq goodsReq){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select id,qr_code,title,model,subject,market_price,sale_price,is_hot,is_recommend,thumbnail_image,");
		sBuffer.append("description from goods where is_onsale = 1 and is_home_page = 1 order by sort_order desc");
		if(goodsReq.getLimit() != null && goodsReq.getLimit() !=0){
			sBuffer.append(" limit #{start},#{limit}");
		}
		return sBuffer.toString();
	}
	
	public String goodsList(final GoodsReq goodsReq){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select g.id,g.qr_code,g.title,g.model,g.subject,g.market_price,g.sale_price,g.is_hot,g.is_recommend,thumbnail_image,");
		sBuffer.append("g.description,g.intr_url from goods g ");
		if(goodsReq.getCategoryId() != null && goodsReq.getCategoryId() != 0){
			sBuffer.append(" left join goods_category gc on g.id = gc.goods_id ");
		}
		
		sBuffer.append(" where 1 = 1 ");
		if(goodsReq.getCategoryId() != null && goodsReq.getCategoryId() != 0){
			sBuffer.append(" and gc.category_id = #{categoryId} ");
		}
		if(null != goodsReq.getKeyword() && !goodsReq.getKeyword().trim().isEmpty()){
			sBuffer.append(" and g.title like '%"+goodsReq.getKeyword().trim()+"%'");
		}
		if(goodsReq.getSortConfig() != null){
			sBuffer.append(goodsReq.getSortConfig());
		}else{
			sBuffer.append("order by g.sort_order desc");	
		}
		if(goodsReq.getLimit() != null && goodsReq.getLimit() !=0){
			sBuffer.append(" limit #{start},#{limit}");
		}
		return sBuffer.toString();
	}
	
	public String goodsListCount(final GoodsReq goodsReq){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select count(1) cnt from goods g ");
		if(goodsReq.getCategoryId() != null && goodsReq.getCategoryId() != 0){
			sBuffer.append(" left join goods_category gc on g.id = gc.goods_id ");
		}
		
		sBuffer.append(" where g.is_onsale = 1 ");
		if(goodsReq.getCategoryId() != null && goodsReq.getCategoryId() != 0){
			sBuffer.append(" and gc.category_id = #{categoryId} ");
		}
		if(null != goodsReq.getKeyword() && !goodsReq.getKeyword().trim().isEmpty()){
			sBuffer.append(" and g.title like '%"+goodsReq.getKeyword().trim()+"%'");
		}
		return sBuffer.toString();
	}
	
	public String medtronicGoodsList(final GoodsReq goodsReq){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select g.id,g.qr_code,g.title,g.model,g.subject,g.market_price,g.sale_price,g.is_hot,g.is_recommend,thumbnail_image,");
		sBuffer.append("g.description,g.intr_url from goods g ");
		if(goodsReq.getCategoryId() != null && goodsReq.getCategoryId() != 0){
			sBuffer.append(" left join goods_category gc on g.id = gc.goods_id ");
		}
		sBuffer.append(" where 1 = 1 ");
		if(goodsReq.getCategoryId() != null && goodsReq.getCategoryId() != 0){
			sBuffer.append(" and gc.category_id = #{categoryId} ");
		}
		if(goodsReq.getSortConfig() != null){
			sBuffer.append(goodsReq.getSortConfig());
		}else{
			sBuffer.append("order by g.sort_order desc");	
		}
		if(goodsReq.getLimit() != null && goodsReq.getLimit() !=0){
			sBuffer.append(" limit #{start},#{limit}");
		}
		return sBuffer.toString();
	}
	
	public String marketingActivityGoodsList(final GoodsReq goodsReq){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select g.id,g.qr_code,g.title,g.model,g.subject,g.market_price,g.sale_price,g.is_hot,g.is_recommend,thumbnail_image,");
		sBuffer.append("g.description,g.intr_url from goods g ,marketing_activity_goods b where g.id = b.goods_id and b.marketing_activity_id = #{marketingActivityId}");
		sBuffer.append(" and g.is_onsale = 1 ");
		if(goodsReq.getSortConfig() != null){
			sBuffer.append(goodsReq.getSortConfig());
		}else{
			sBuffer.append("order by g.sort_order desc");	
		}
		if(goodsReq.getLimit() != null && goodsReq.getLimit() !=0){
			sBuffer.append(" limit #{start},#{limit}");
		}
		return sBuffer.toString();
	}
	
	public String managerList(GoodsReq goodsReq){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select * from goods a where 1 = 1 ");
		if(goodsReq.getTitle() != null && !goodsReq.getTitle().trim().isEmpty()){
			sBuffer.append(" and a.title like concat('%',#{title},'%') ");
		}
		if(goodsReq.getIsOnsale() != null){
			sBuffer.append(" and a.is_onsale = #{isOnsale} ");
		}
		sBuffer.append(" order by a.sort_order ");
		if(goodsReq.getLimit() != null){
			sBuffer.append(" limit #{start},#{limit} ");
		}
		return sBuffer.toString();
	}
	
	public String updateCatetory(Category category){
		StringBuffer sb = new StringBuffer();
		sb.append("update category set last_modify_time = current_timestamp() ");
		if(category.getTitle() != null && !category.getTitle().trim().isEmpty()){
			sb.append(" ,title = #{title}");
		}
		if(category.getSortOrder() != null){
			sb.append(" ,sort_order = #{sortOrder}");
		}
		if(category.getLevel() != null && category.getLevel() != 0){
			sb.append(" ,level = #{level}");
		}
		if(category.getParentId() != null){
			sb.append(" ,parent_id = #{parentId}");
		}
		if(category.getDisplayCol() != null){
			sb.append(" ,display_col = #{displayCol}");
		}
		sb.append(" where id = #{id}");
		return sb.toString();
	}
	
	
	/**
 	* 新增商品
 	*/
 	public String insert(Goods domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into goods(create_time,last_modify_time");
	 	if(domain.getQrCode()!=null){
			sb.append(",qr_code");
		}
	 	if(domain.getCatId()!=null){
			sb.append(",cat_id");
		}
	 	if(domain.getTitle()!=null){
			sb.append(",title");
		}
	 	if(domain.getModel()!=null){
			sb.append(",model");
		}
	 	if(domain.getSku()!=null){
			sb.append(",sku");
		}
	 	if(domain.getSupplierId()!=null){
			sb.append(",supplier_id");
		}
	 	if(domain.getStockId()!=null){
			sb.append(",stock_id");
		}
	 	if(domain.getLocation()!=null){
			sb.append(",location");
		}
	 	if(domain.getSubject()!=null){
			sb.append(",subject");
		}
	 	if(domain.getDescription()!=null){
			sb.append(",description");
		}
	 	if(domain.getMarketPrice()!=null){
			sb.append(",market_price");
		}
	 	if(domain.getSalePrice()!=null){
			sb.append(",sale_price");
		}
	 	if(domain.getAmount()!=null){
			sb.append(",amount");
		}
	 	if(domain.getIsReal()!=null){
			sb.append(",is_real");
		}
	 	if(domain.getSortOrder()!=null){
			sb.append(",sort_order");
		}
	 	if(domain.getIsHot()!=null){
			sb.append(",is_hot");
		}
	 	if(domain.getIsRecommend()!=null){
			sb.append(",is_recommend");
		}
	 	if(domain.getThumbnailImage()!=null){
			sb.append(",thumbnail_image");
		}
	 	if(domain.getStockStatus()!=null){
			sb.append(",stock_status");
		}
	 	if(domain.getIsOnsale()!=null){
			sb.append(",is_onsale");
		}
	 	if(domain.getSubtract()!=null){
			sb.append(",subtract");
		}
	 	if(domain.getPostFree()!=null){
			sb.append(",post_free");
		}
	 	if(domain.getIsHomePage()!=null){
			sb.append(",is_home_page");
		}
	 	if(domain.getBuyCount()!=null){
			sb.append(",buy_count");
		}
	 	if(domain.getIntrUrl()!=null){
			sb.append(",intr_url");
		}
	 	if(domain.getIsExpress()!=null){
			sb.append(",is_express");
		}
	 	if(domain.getGoodsType()!=null){
			sb.append(",goods_type");
		}
	 	if(domain.getIsUseCoupon()!=null){
			sb.append(",is_use_coupon");
		}
	 	if(domain.getManufacturer()!=null){
			sb.append(",manufacturer");
		}
	 	if(domain.getApprovalNumber()!=null){
			sb.append(",approval_number");
		}
	 	if(domain.getBrand()!=null){
			sb.append(",brand");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getQrCode()!=null){
			sb.append(",#{qrCode}");
		}
	 	if(domain.getCatId()!=null){
			sb.append(",#{catId}");
		}
	 	if(domain.getTitle()!=null){
			sb.append(",#{title}");
		}
	 	if(domain.getModel()!=null){
			sb.append(",#{model}");
		}
	 	if(domain.getSku()!=null){
			sb.append(",#{sku}");
		}
	 	if(domain.getSupplierId()!=null){
			sb.append(",#{supplierId}");
		}
	 	if(domain.getStockId()!=null){
			sb.append(",#{stockId}");
		}
	 	if(domain.getLocation()!=null){
			sb.append(",#{location}");
		}
	 	if(domain.getSubject()!=null){
			sb.append(",#{subject}");
		}
	 	if(domain.getDescription()!=null){
			sb.append(",#{description}");
		}
	 	if(domain.getMarketPrice()!=null){
			sb.append(",#{marketPrice}");
		}
	 	if(domain.getSalePrice()!=null){
			sb.append(",#{salePrice}");
		}
	 	if(domain.getAmount()!=null){
			sb.append(",#{amount}");
		}
	 	if(domain.getIsReal()!=null){
			sb.append(",#{isReal}");
		}
	 	if(domain.getSortOrder()!=null){
			sb.append(",#{sortOrder}");
		}
	 	if(domain.getIsHot()!=null){
			sb.append(",#{isHot}");
		}
	 	if(domain.getIsRecommend()!=null){
			sb.append(",#{isRecommend}");
		}
	 	if(domain.getThumbnailImage()!=null){
			sb.append(",#{thumbnailImage}");
		}
	 	if(domain.getStockStatus()!=null){
			sb.append(",#{stockStatus}");
		}
	 	if(domain.getIsOnsale()!=null){
			sb.append(",#{isOnsale}");
		}
	 	if(domain.getSubtract()!=null){
			sb.append(",#{subtract}");
		}
	 	if(domain.getPostFree()!=null){
			sb.append(",#{postFree}");
		}
	 	if(domain.getIsHomePage()!=null){
			sb.append(",#{isHomePage}");
		}
	 	if(domain.getBuyCount()!=null){
			sb.append(",#{buyCount}");
		}
	 	if(domain.getIntrUrl()!=null){
			sb.append(",#{intrUrl}");
		}
	 	if(domain.getIsExpress()!=null){
			sb.append(",#{isExpress}");
		}
	 	if(domain.getGoodsType()!=null){
			sb.append(",#{goodsType}");
		}
	 	if(domain.getIsUseCoupon()!=null){
			sb.append(",#{isUseCoupon}");
		}
	 	if(domain.getManufacturer()!=null){
			sb.append(",#{manufacturer}");
		}
	 	if(domain.getApprovalNumber()!=null){
			sb.append(",#{approvalNumber}");
		}
	 	if(domain.getBrand()!=null){
			sb.append(",#{brand}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新商品 
 	*/
	public String update(Goods domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update goods set last_modify_time = now() ");
		
		if(domain.getQrCode()!=null){
			sb.append(",qr_code=#{qrCode}");
		}
		if(domain.getCatId()!=null){
			sb.append(",cat_id=#{catId}");
		}
		if(domain.getTitle()!=null){
			sb.append(",title=#{title}");
		}
		if(domain.getModel()!=null){
			sb.append(",model=#{model}");
		}
		if(domain.getSku()!=null){
			sb.append(",sku=#{sku}");
		}
		if(domain.getSupplierId()!=null){
			sb.append(",supplier_id=#{supplierId}");
		}
		if(domain.getStockId()!=null){
			sb.append(",stock_id=#{stockId}");
		}
		if(domain.getLocation()!=null){
			sb.append(",location=#{location}");
		}
		if(domain.getSubject()!=null){
			sb.append(",subject=#{subject}");
		}
		if(domain.getDescription()!=null){
			sb.append(",description=#{description}");
		}
		if(domain.getMarketPrice()!=null){
			sb.append(",market_price=#{marketPrice}");
		}
		if(domain.getSalePrice()!=null){
			sb.append(",sale_price=#{salePrice}");
		}
		if(domain.getAmount()!=null){
			sb.append(",amount=#{amount}");
		}
		if(domain.getIsReal()!=null){
			sb.append(",is_real=#{isReal}");
		}
		if(domain.getSortOrder()!=null){
			sb.append(",sort_order=#{sortOrder}");
		}
		if(domain.getIsHot()!=null){
			sb.append(",is_hot=#{isHot}");
		}
		if(domain.getIsRecommend()!=null){
			sb.append(",is_recommend=#{isRecommend}");
		}
		if(domain.getThumbnailImage()!=null){
			sb.append(",thumbnail_image=#{thumbnailImage}");
		}
		if(domain.getStockStatus()!=null){
			sb.append(",stock_status=#{stockStatus}");
		}
		if(domain.getIsOnsale()!=null){
			sb.append(",is_onsale=#{isOnsale}");
		}
		if(domain.getSubtract()!=null){
			sb.append(",subtract=#{subtract}");
		}
		if(domain.getPostFree()!=null){
			sb.append(",post_free=#{postFree}");
		}
		if(domain.getIsHomePage()!=null){
			sb.append(",is_home_page=#{isHomePage}");
		}
		if(domain.getBuyCount()!=null){
			sb.append(",buy_count=#{buyCount}");
		}
		if(domain.getIntrUrl()!=null){
			sb.append(",intr_url=#{intrUrl}");
		}
		if(domain.getIsExpress()!=null){
			sb.append(",is_express=#{isExpress}");
		}
		if(domain.getGoodsType()!=null){
			sb.append(",goods_type=#{goodsType}");
		}
		if(domain.getIsUseCoupon()!=null){
			sb.append(",is_use_coupon=#{isUseCoupon}");
		}
		if(domain.getManufacturer()!=null){
			sb.append(",manufacturer=#{manufacturer}");
		}
		if(domain.getApprovalNumber()!=null){
			sb.append(",approval_number=#{approvalNumber}");
		}
		if(domain.getBrand()!=null){
			sb.append(",brand=#{brand}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	/**
 	* 新增 商品分类关系表,一个商品可以在多个分类下
 	*/
 	public String insertGoodsCategory(GoodsCategory domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into goods_category(create_time,last_modify_time");
	 	if(domain.getCategoryId()!=null){
			sb.append(",category_id");
		}
	 	if(domain.getGoodsId()!=null){
			sb.append(",goods_id");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getCategoryId()!=null){
			sb.append(",#{categoryId}");
		}
	 	if(domain.getGoodsId()!=null){
			sb.append(",#{goodsId}");
		}
		sb.append(")");
		
		return sb.toString();
	}

}
